package com.shuwang.jh_shop.ui.base

import android.os.Build
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.MenuItem
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import com.shuwang.jh_shop.R
import com.shuwang.jh_shop.utils.AppManager

/**
 * 作者  格锅
 * 时间  2017/4/5 15:41
 * 文件  Tf_app
 * 描述  Activity栈
 * 1. 吐司
 */
abstract class BaseActivity: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        AppManager.appManager.addActivity(this)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上 让系统栏颜色透明
            val decorView = window.decorView
            var option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            decorView.systemUiVisibility = option
            window.statusBarColor= ContextCompat.getColor(applicationContext, R.color.colorPrimary)
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
             val localLayoutParams = window.attributes
             localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or localLayoutParams.flags)
        }
        setContentView(getLayoutId())
        initView()
        initData()
    }

    fun setToolbar(toolbar: Toolbar){
        setSupportActionBar(toolbar)
        supportActionBar!!.title=""
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        supportActionBar!!.setHomeAsUpIndicator(R.drawable.indicator)
    }
    override fun onDestroy() {
        super.onDestroy()
        AppManager.appManager.removeActivity(this)
    }

    abstract fun initView()
    abstract fun getLayoutId():Int
    abstract fun initData()

    protected fun showToast(msg:String,flag:Int= Toast.LENGTH_SHORT){
        Toast.makeText(this,msg,flag).show()
    }


    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item?.itemId==android.R.id.home){
            this.finish()
            return true
        }
        return super.onOptionsItemSelected(item)
    }

}